/**
 * @file J1939_object.c
 * @author Jomb (jomb.liu@solinftec.com)
 * @brief
 * @version 0.1
 * @date 2022-06-08
 *
 * @copyright Copyright (c) 2022
 *
 */
#include "j1939_object.h"

#define SRC_ADDRESS 0X11
#define DESK_JETSON_ADDRESS 0X1c

/*================================================================*/

static struct
{
    unsigned short factor;
    unsigned short offset;
    unsigned short max;
    unsigned short min;
    unsigned short obj;
} control_cmd = {1, 0, 0, 0, 0};

static struct
{
    float factor;
    float offset;
    float max;
    float min;
    float obj;
} target_velocity = {0.01f, 0.0f, 0.0f, 0.0f, 0.0f};

static struct
{
    float factor;
    float offset;
    float max;
    float min;
    float obj;
} target_palstance = {0.001f, 0.0f, 0.0f, 0.0f, 0.0f};

static struct
{
    unsigned short factor;
    unsigned short offset;
    unsigned short max;
    unsigned short min;
    unsigned short obj;
} status_cmd = {1, 0, 0, 0, 0};

static struct
{
    float factor;
    float offset;
    float max;
    float min;
    float obj;
} velocity_fb = {0.01f, 0.0f, 0.0f, 0.0f, 0.0f};

static struct
{
    float factor;
    float offset;
    float max;
    float min;
    float obj;
} palstance_fb = {0.001f, 0.0f, 0.0f, 0.0f, 0.0f};

/*================================================================*/

static j1939_doubtful_object sub_pgn_idx0[] = {
    {0, 0, 16, J1939_UINT16_T, &control_cmd.factor, &control_cmd.offset,
     &control_cmd.max, &control_cmd.min, &control_cmd.obj, DESK_JETSON_ADDRESS},
    {1, 16, 16, J1939_FLOAT_T, &target_velocity.factor, &target_velocity.offset,
     &target_velocity.max, &target_velocity.min, &target_velocity.obj, DESK_JETSON_ADDRESS},
    {2, 32, 16, J1939_FLOAT_T, &target_palstance.factor, &target_palstance.offset,
     &target_palstance.max, &target_palstance.min, &target_palstance.obj, DESK_JETSON_ADDRESS}};

static j1939_spn_callback_t pgn_idx0_cb[] = {
    NULL, NULL, NULL};

static j1939_doubtful_object sub_pgn_idx1[] = {
    {3, 0, 16, J1939_UINT16_T, &status_cmd.factor, &status_cmd.offset,
     &status_cmd.max, &status_cmd.min, &status_cmd.obj, SRC_ADDRESS},
    {4, 16, 16, J1939_FLOAT_T, &velocity_fb.factor, &velocity_fb.offset,
     &velocity_fb.max, &velocity_fb.min, &velocity_fb.obj, SRC_ADDRESS},
    {5, 32, 16, J1939_FLOAT_T, &palstance_fb.factor, &target_palstance.offset,
     &palstance_fb.max, &palstance_fb.min, &palstance_fb.obj, SRC_ADDRESS}};

static j1939_spn_callback_t pgn_idx1_cb[] = {
    NULL, NULL, NULL};

/*================================================================*/
static const j1939_param_group png_group[] = {
    {(0x00 << 8) | 0x11, sizeof(sub_pgn_idx0) / sizeof(j1939_doubtful_object), sub_pgn_idx0},
    {(0x01 << 8) | 0x1c, sizeof(sub_pgn_idx1) / sizeof(j1939_doubtful_object), sub_pgn_idx1},
};

/**
 * @brief
 *
 * @param pgn
 * @param err_code
 * @param cb
 * @return const j1939_param_group*
 */
const j1939_param_group *j1939_scan_png_group(unsigned int pgn, int *err_code, j1939_spn_callback_t **cb)
{
    int i = 0;
    *cb = NULL;

    switch (pgn)
    {
    case (0x00 << 8) | 0x11:
    {
        i = 0;
        *cb = pgn_idx0_cb;
    }
    break;
    case (0x01 << 8) | 0x1c:
    {
        i = 1;
        *cb = pgn_idx1_cb;
    }
    break;
    default:
    {
        *err_code = -1;
        LOG_E(0X2000, "Invalid parameter of pgn", pgn);
        return NULL;
    }
    }
    *err_code = 0;
    return &png_group[i];
}
